# -*- coding: utf-8 -*-
import ast, json
from uuid import uuid4

import ast

from django.views import View
# 导入返回响应
from SaaSDevelTool.data_return_response import create_response
# 导入错误信息装饰器
from SaaSDevelTool.data_return_response import error_message_detection
# 获取数据库连接
from SaaSDevelTool.data_mysql_data import db

"""
页面权限展示
"""


class SaaSDevelResourceRightQuery(View):
    @error_message_detection
    def get(self, request):
        # 获取对应的页面编号
        resource_id = request.GET.get("resource_id")
        # 获取权限类型(0,1,2)
        right_type = request.GET.get("right_type")
        # 获取名称
        right_name = request.GET.get("right_name")
        # 判断参数是否完整
        if not all([resource_id, right_type]):
            return create_response(code=400, msg="必传参数未传递完整。")

        # 设置当前查询条件
        query_where_sql = """"""

        # 判断名称是否存在
        if right_name:
            query_where_sql = query_where_sql + f"""AND 
            right_name = '{right_name}'"""
        else:
            query_where_sql = query_where_sql

        # 获取对应的数据
        query_base_info_resource_right_sql = """
            select
                right_id,
                right_name,
                right_code,
                right_level,
                resource_id,
                right_type
            from
                db_saas_center.sum_page_resource_right 
            where 
                right_type = {} 
            {}
            ;""".format(right_type, query_where_sql)
        query_base_info_resource_right_data = db.findAll(query_base_info_resource_right_sql)

        # 定义空列表用于存储结果
        parsed_data_list = []

        # 遍历原始数据
        for item in query_base_info_resource_right_data:
            # 解析"right_level"字符串
            levels = eval(item['right_level'])
            # 构建新的字典列表
            for level in levels:
                parsed_data_list.append({
                    'right_id': item['right_id'],
                    'right_name': item['right_name'],
                    'name': level['name'],
                    'grade': level['grade'],
                    'right_type': item['right_type']
                })
        return create_response(code=200, msg="", data=parsed_data_list)
